package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.bean.AddDeviceMethodBean;
import com.qiantu.phone.ui.adapter.AddDeviceStepAdapter;
import com.qiantu.phone.ui.dialog.BottomCodeAddDialog;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 */
public final class AddDevice2Activity extends AppActivity {

    private RecyclerView recyclerView;
    private CheckBox checkBox;
    private View btnNext;
    private AddDeviceStepAdapter mAddDeviceStepAdapter;
    private ArrayList<AddDeviceMethodBean> links;
    private int productLinkType;
    //产品类型
    private String productModel;
    private String addDeviceName;
    private BottomCodeAddDialog mBottomCodeAddDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_device2;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        checkBox = findViewById(R.id.check_box);
        btnNext = findViewById(R.id.btn_next);
        setOnClickListener(btnNext);
        links = getIntent().getParcelableArrayListExtra("links");
        addDeviceName = getIntent().getStringExtra("addDeviceName");
        productModel = getIntent().getStringExtra("productModel");
        productLinkType = getIntent().getIntExtra("productLinkType", 0);
        if (addDeviceName.equals("1")){
            setTitle(getString(R.string.add_wifi_network));
        }else {
            setTitle(getString(R.string.add_device_holder, addDeviceName));
        }

        initListView();
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btnNext.setEnabled(isChecked);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        btnNext.setEnabled(checkBox.isChecked());
    }

    private void initListView() {
        if(mAddDeviceStepAdapter == null){
            mAddDeviceStepAdapter = new AddDeviceStepAdapter(this);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(mAddDeviceStepAdapter);
        }
        for(AddDeviceMethodBean addDeviceMethodBean : links){
            if(addDeviceMethodBean.getProductLinkType() == productLinkType){
                mAddDeviceStepAdapter.setData(addDeviceMethodBean.getLinkSteps());
                break;
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_next:
                Intent intent = null;
                switch (productLinkType) {
                    case AddDevice1Activity.TYPE_WIRED_ADD:
                        if (productModel.equals("DS-X")){
                            showCodeLog();
                        }else {
                            intent = new Intent(this, AddDevice3Activity.class);
                            intent.putExtra("deviceSn", getIntent().getStringExtra("deviceSn"));
                            intent.putExtra("productLinkType", productLinkType);
                            startActivity(intent);
                        }

                        break;
                    case AddDevice1Activity.TYPE_WIFI_ADD:
                        intent = new Intent(this, SetNetWorkWifiActivity.class);
                        for (AddDeviceMethodBean addDeviceMethodBean : links) {
                            if (addDeviceMethodBean.getProductLinkType() == AddDevice1Activity.TYPE_WIRED_ADD) {//有线配网
                                intent.putExtra("hasWiredAdd", true);
                                break;
                            }
                        }
                        //重联网判断
                        if (addDeviceName.equals("1")) {
                            intent.putExtra("hasWiredAdd", false);
                        }
                        startActivity(intent);
                        break;
                }
        }
    }
//    可视对讲弹出验证码
    private void showCodeLog(){
        if (mBottomCodeAddDialog == null) {
            mBottomCodeAddDialog = new BottomCodeAddDialog(this);
            mBottomCodeAddDialog.setDialogClickListener(new BottomCodeAddDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(String code) {
                    //操作设备码
                    Intent intent = new Intent(AddDevice2Activity.this, AddDevice3Activity.class);
                    intent.putExtra("deviceSn", getIntent().getStringExtra("deviceSn"));
                    intent.putExtra("productLinkType", productLinkType);
                    intent.putExtra("validateCode", code);
                    startActivity(intent);

                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomCodeAddDialog);
        }
        mBottomCodeAddDialog.show();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        productLinkType = intent.getIntExtra("productLinkType", 0);
        initListView();
    }

    @Override
    protected void initData() {
    }
}